<?php
$cache_time=10;
$OJ_CACHE_SHARE=false;
require_once("config/path_config.php");
require_once('./include/cache_start.php');
require_once('./include/db_info.inc.php');
require_once('./include/my_func.inc.php');
require_once('./include/setlang.php');
require_once("./include/const.inc.php");
require_once("./config/privliege_config.php");

$view_title= "题库-程序源码";

if (!isset($_GET['id'])){
	$view_errors= "未查询到源码!\n";
	require("template/bs3/error.php");
	exit(0);
}

// 查询指定ID的提交记录
$id = intval($_GET['id']);
$sql="SELECT * FROM `solution` WHERE `solution_id`=?";
$result = pdo_query($sql, $id);
$row = null;
if ( $result ) {
	$row = $result[0];
}
if ( $row == null ) {
    $view_errors= "未查询到源码!\n";
    require("template/bs3/error.php");
    exit(0);
}

// 代码提交信息
$slanguage=$row['language'];
$sresult=$row['result'];
$stime=$row['time'];
$smemory=$row['memory'];
$sproblem_id=$row['problem_id'];
$view_user_id= $suser_id= $row['user_id'];
$contest_id=$row['contest_id'];

// 权限判断
$isAllowViewSource = false;
// 1. 用户已登录,并且提交代码的用户即为当前登录用户
$sessionUserId = "";
if ( isset($_SESSION['user_id']) ) {
    $sessionUserId = strtolower($_SESSION['user_id']);
    $sourceUserId = strtolower($row['user_id']);
    $isAllowViewSource |= ($sessionUserId == $sourceUserId);
}

// 2. 用户已登录,并且AC过本题
if ( ! $isAllowViewSource && isset($_SESSION['viewcode_after_ac']) ) {
    $isAllowViewSource |= check_ac_problem($sproblem_id);
}

// 3. 用户已登录,并且具备查看代码的权限
if ( ! $isAllowViewSource ) {
    $isAllowViewSource = isset($_SESSION['user_id']) && isset($_SESSION['viewcode_no_ac']);
}

// 4. 用户是管理员
if (! $isAllowViewSource ) {
	$isAllowViewSource = isset($_SESSION['administrator']) && $_SESSION['administrator'];
}

// 5. 用户是教师
if (! $isAllowViewSource ) {
    $isAllowViewSource = isset($_SESSION['teacher']) && $_SESSION['teacher'];
}

// 6. 用户是学生父母
if (! $isAllowViewSource ) {
    $isAllowViewSource = isset($_SESSION['parent']) && $_SESSION['parent'];
}

// 6. 用户是学生父母
if (! $isAllowViewSource ) {
    $isAllowViewSource = isset($_SESSION['parent']) && $_SESSION['parent'];
}

if ( !$isAllowViewSource ) {
    $view_errors= "你不具备查看该内容的权限";
    require("template/bs3/error.php");
    exit(0);
}

// 是否允许编辑, teacher, administrator允许编辑自己提交的代码
$isAllowEdit =  isset($_SESSION['teacher']) && $_SESSION['teacher'];
$isAllowEdit |= isset($_SESSION['administrator']) || $_SESSION['administrator'];
if ( $isAllowEdit ) {
    $sessionUserId = strtolower($_SESSION['user_id']);
    $sourceUserId = strtolower($row['user_id']);
    $isAllowEdit = ($sessionUserId == $sourceUserId);
}

// 查询题目信息
$sql = "select `title`, `defunct`, `description` from problem where problem_id=?";
$result=pdo_query($sql, $sproblem_id);
$row = null;
if ( $result ) {
    $row=$result[0];
}
if ( $row == null ) {
    $view_errors= "<h2>题目不存在</h2>";
    require("template/bs3/error.php");
    exit(0);
}

$is_problem_valid = false;
if( $row ) {
    $view_p_title=$row['title'];
    $view_p_desc=$row['description'];
    $is_problem_valid = $row['defunct']=='N';
}

// 非管理员无权查看未启用的题目
$isAdmin = isset($_SESSION['administrator']);
if ( ! $isAdmin && !$is_problem_valid ) {
    $view_errors= "<h2>你不具备查看该内容的权限</h2>";
    require("template/".$OJ_TEMPLATE."/error.php");
    exit(0);
}

if ( $isAllowViewSource  ) {
    // 查询指定提交ID对应的源代码
    $sql="SELECT `source` FROM `source_code_user` WHERE `solution_id`=?";
    $result=pdo_query($sql, $id);
    $row = null;
    if ( $result ) {
        $row=$result[0];
	}
    if( $row ) {
        $view_source=$row['source'];
	}
}

/////////////////////////Template
require("template/bs3/showsource.php");
/////////////////////////Common foot
if(file_exists('./include/cache_end.php'))
	require_once('./include/cache_end.php');
?>

